<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">




<script>
    (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
  </script>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />



  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico?v=5.1.4">







  <meta name="keywords" content="SwiftUI," />





  <link rel="alternate" href="/atom.xml" title="Swift社区" type="application/atom+xml" />






<meta name="description" content="前言在本文中，我们将深入探讨一些创建 SwiftUI 动画的高级技术。我将广泛讨论 Animatable 协议，它可靠的伙伴 animatableData，强大但经常被忽略的 GeometryEffect 以及完全被忽视但全能的 AnimatableModifier 协议。 这些都是被官方文档完全忽略的主题，在SwiftUI 的帖子和文章中也几乎没有提及。不过，它们还是为我们提供了创建一些相当不">
<meta name="keywords" content="SwiftUI">
<meta property="og:type" content="article">
<meta property="og:title" content="高级 SwiftUI 动画 — Part 1：Paths">
<meta property="og:url" content="https://fanbaoying.github.io/高级-SwiftUI-动画-—-Part-1：Paths/index.html">
<meta property="og:site_name" content="Swift社区">
<meta property="og:description" content="前言在本文中，我们将深入探讨一些创建 SwiftUI 动画的高级技术。我将广泛讨论 Animatable 协议，它可靠的伙伴 animatableData，强大但经常被忽略的 GeometryEffect 以及完全被忽视但全能的 AnimatableModifier 协议。 这些都是被官方文档完全忽略的主题，在SwiftUI 的帖子和文章中也几乎没有提及。不过，它们还是为我们提供了创建一些相当不">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/28c53aa3fd2e980731e70075a1857a27.png">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/d06e59d9851af539adf53d2a90bdd2df.gif">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/e5d5999cb7891fa51bd59609c7cb190f.gif">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/af3cc99556499081e410cfed1a59cf16.png">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/c12634ccd556d04558154aa3e50ba902.gif">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/a9d74ce65e124f4181701f5f5616253e.gif">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/cc060bf316c70d480d9c64dcf75f562d.gif">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/631640b6fd2099e4ccd9bcce295dade7.gif">
<meta property="og:image" content="https://images.xiaozhuanlan.com/photo/2022/1f7298958b84e19cf6b5ca133e2c8fd5.gif">
<meta property="og:updated_time" content="2022-04-06T02:38:36.974Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="高级 SwiftUI 动画 — Part 1：Paths">
<meta name="twitter:description" content="前言在本文中，我们将深入探讨一些创建 SwiftUI 动画的高级技术。我将广泛讨论 Animatable 协议，它可靠的伙伴 animatableData，强大但经常被忽略的 GeometryEffect 以及完全被忽视但全能的 AnimatableModifier 协议。 这些都是被官方文档完全忽略的主题，在SwiftUI 的帖子和文章中也几乎没有提及。不过，它们还是为我们提供了创建一些相当不">
<meta name="twitter:image" content="https://images.xiaozhuanlan.com/photo/2022/28c53aa3fd2e980731e70075a1857a27.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://fanbaoying.github.io/高级-SwiftUI-动画-—-Part-1：Paths/"/>





  <title>高级 SwiftUI 动画 — Part 1：Paths | Swift社区</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
    
    <a href="https://github.com/SwiftCommunityRes" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Swift社区</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">做最好的 Swift 社区</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://fanbaoying.github.io/高级-SwiftUI-动画-—-Part-1：Paths/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Swift社区">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://avatars.githubusercontent.com/u/84354365?v=4">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Swift社区">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">高级 SwiftUI 动画 — Part 1：Paths</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-04-06T10:30:47+08:00">
                2022-04-06
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/SwiftUI-动画/" itemprop="url" rel="index">
                    <span itemprop="name">SwiftUI 动画</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                -->
                <span title="字数统计">
                  数字统计 4,150字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                --> 

                <span title="阅读时长">
                  阅读时长 17分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://images.xiaozhuanlan.com/photo/2022/28c53aa3fd2e980731e70075a1857a27.png" alt=""></p>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>在本文中，我们将深入探讨一些创建 SwiftUI 动画的高级技术。我将广泛讨论 <a href="https://developer.apple.com/documentation/swiftui/animatable" target="_blank" rel="noopener">Animatable</a> 协议，它可靠的伙伴 <a href="https://developer.apple.com/documentation/swiftui/animatable/3046497-animatabledata" target="_blank" rel="noopener">animatableData</a>，强大但经常被忽略的 <a href="https://developer.apple.com/documentation/swiftui/geometryeffect" target="_blank" rel="noopener">GeometryEffect</a> 以及完全被忽视但全能的 <a href="https://developer.apple.com/documentation/swiftui/animatablemodifier" target="_blank" rel="noopener">AnimatableModifier</a> 协议。</p>
<p>这些都是被官方文档完全忽略的主题，在SwiftUI 的帖子和文章中也几乎没有提及。不过，它们还是为我们提供了创建一些相当不错的动画的工具。</p>
<p>在我们进入这些隐藏的瑰宝之前，我想对一些基本的 SwiftUI 动画概念做一个非常快速的总结。只是为了让我们能有共同语言，请耐心听我说。</p>
<a id="more"></a>
<h1 id="显式动画-VS-隐式动画"><a href="#显式动画-VS-隐式动画" class="headerlink" title="显式动画 VS 隐式动画"></a>显式动画 VS 隐式动画</h1><p>在SwiftUI中，有两种类型的动画。显式和隐式。<strong>隐式动画</strong>是你用 <code>.animation()</code> 修饰符指定的那些动画。每当视图上的可动画参数发生变化时，SwiftUI 就会从旧值到新值制作动画。一些可动画的参数包括大小(size)、偏移(offset)、颜色(color)、比例(scale)等。</p>
<p>显式动画是使用 <code>withAnimation{ … }</code> 指定的动画闭包。只有那些依赖于 <code>withAnimation</code> 闭包中改变值的参数才会被动画化。让我们尝试举一些例子来说明：</p>
<p>以下示例使用隐式动画更改图像的大小和不透明度：<br><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Example1</span>: <span class="title">View</span> </span>&#123;</span><br><span class="line">    @<span class="type">State</span> <span class="keyword">private</span> <span class="keyword">var</span> half = <span class="literal">false</span></span><br><span class="line">    @<span class="type">State</span> <span class="keyword">private</span> <span class="keyword">var</span> dim = <span class="literal">false</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">var</span> body: some <span class="type">View</span> &#123;</span><br><span class="line">        <span class="type">Image</span>(<span class="string">"tower"</span>)</span><br><span class="line">            .scaleEffect(half ? <span class="number">0.5</span> : <span class="number">1.0</span>)</span><br><span class="line">            .opacity(dim ? <span class="number">0.2</span> : <span class="number">1.0</span>)</span><br><span class="line">            .animation(.easeInOut(duration: <span class="number">1.0</span>))</span><br><span class="line">            .onTapGesture &#123;</span><br><span class="line">                <span class="keyword">self</span>.dim.toggle()</span><br><span class="line">                <span class="keyword">self</span>.half.toggle()</span><br><span class="line">            &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><img src="https://images.xiaozhuanlan.com/photo/2022/d06e59d9851af539adf53d2a90bdd2df.gif" alt=""></p>
<p>下面的示例使用显式动画。在这里，缩放和不透明度都会更改，但只有不透明度会设置动画，因为它是 <code>withAnimation</code> 闭包中唯一更改的参数：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Example2</span>: <span class="title">View</span> </span>&#123;</span><br><span class="line">    @<span class="type">State</span> <span class="keyword">private</span> <span class="keyword">var</span> half = <span class="literal">false</span></span><br><span class="line">    @<span class="type">State</span> <span class="keyword">private</span> <span class="keyword">var</span> dim = <span class="literal">false</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">var</span> body: some <span class="type">View</span> &#123;</span><br><span class="line">        <span class="type">Image</span>(<span class="string">"tower"</span>)</span><br><span class="line">            .scaleEffect(half ? <span class="number">0.5</span> : <span class="number">1.0</span>)</span><br><span class="line">            .opacity(dim ? <span class="number">0.5</span> : <span class="number">1.0</span>)</span><br><span class="line">            .onTapGesture &#123;</span><br><span class="line">                <span class="keyword">self</span>.half.toggle()</span><br><span class="line">                </span><br><span class="line">                withAnimation(.easeInOut(duration: <span class="number">1.0</span>)) &#123;</span><br><span class="line">                    <span class="keyword">self</span>.dim.toggle()</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://images.xiaozhuanlan.com/photo/2022/e5d5999cb7891fa51bd59609c7cb190f.gif" alt=""></p>
<p>请注意，通过更改修饰符的前后顺序，可以使用隐式动画创建相同的效果：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Example2</span>: <span class="title">View</span> </span>&#123;</span><br><span class="line">    @<span class="type">State</span> <span class="keyword">private</span> <span class="keyword">var</span> half = <span class="literal">false</span></span><br><span class="line">    @<span class="type">State</span> <span class="keyword">private</span> <span class="keyword">var</span> dim = <span class="literal">false</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">var</span> body: some <span class="type">View</span> &#123;</span><br><span class="line">        <span class="type">Image</span>(<span class="string">"tower"</span>)</span><br><span class="line">            .opacity(dim ? <span class="number">0.2</span> : <span class="number">1.0</span>)</span><br><span class="line">            .animation(.easeInOut(duration: <span class="number">1.0</span>))</span><br><span class="line">            .scaleEffect(half ? <span class="number">0.5</span> : <span class="number">1.0</span>)</span><br><span class="line">            .onTapGesture &#123;</span><br><span class="line">                <span class="keyword">self</span>.dim.toggle()</span><br><span class="line">                <span class="keyword">self</span>.half.toggle()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果需要禁用动画，可以使用 <code>.animation(nil)</code>。</p>
<h2 id="动画是如何工作的"><a href="#动画是如何工作的" class="headerlink" title="动画是如何工作的"></a>动画是如何工作的</h2><p>在所有SwiftUI动画的背后，有一个名为 <strong>Animatable</strong> 的协议。我们将在后面讨论细节，但主要是，它拥有一个计算属性，其类型遵守 <code>VectorArithmetic</code> 协议。这使得框架可以随意地插值。</p>
<p>当给一个视图制作动画时，SwiftUI 实际上是多次重新生成该视图，并且每次都修改动画参数。这样，它就会从原点值渐渐走向最终值。</p>
<p>假设我们为一个视图的不透明度创建一个线性动画。我们打算从 0.3 到 0.8。该框架将多次重新生成视图，以小幅度的增量来改变不透明度。由于不透明度是以 <code>Double</code>表示的，而且<code>Double</code> 遵守 VectorArithmetic` 协议，SwiftUI 可以插值出所需的不透明度值。在框架代码的某个地方，可能有一个类似的算法。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> from:<span class="type">Double</span> = <span class="number">0.3</span></span><br><span class="line"><span class="keyword">let</span> to:<span class="type">Double</span> = <span class="number">0.8</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="number">0</span>..&lt;<span class="number">6</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> pct = <span class="type">Double</span>(i) / <span class="number">5</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">var</span> difference = to - from</span><br><span class="line">    difference.scale(by: pct)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">let</span> currentOpacity = from + difference</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">print</span>(<span class="string">"currentOpacity = <span class="subst">\(currentOpacity)</span>"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>代码将创建从起点到终点的渐进式更改：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">currentOpacity = 0.3</span><br><span class="line">currentOpacity = 0.4</span><br><span class="line">currentOpacity = 0.5</span><br><span class="line">currentOpacity = 0.6</span><br><span class="line">currentOpacity = 0.7</span><br><span class="line">currentOpacity = 0.8</span><br></pre></td></tr></table></figure>
<h1 id="为什么我关心-Animatable？"><a href="#为什么我关心-Animatable？" class="headerlink" title="为什么我关心 Animatable？"></a>为什么我关心 <code>Animatable</code>？</h1><p>你可能会问，为什么我需要关心所有这些小细节。SwiftUI 已经为不透明度制作了动画，而不需要我担心这一切。是的，这是真的，但只要 SwiftUI 知道如何将数值从原点插值到终点。对于不透明度，这是一个直接的过程，SwiftUI 知道该怎么做。然而，正如我们接下来要看到的，情况并非总是如此。</p>
<p>我想到了一些大的例外情况：路径(paths)、变换矩阵(matrices)和任意的视图变化（例如，文本视图中的文本、渐变视图中的渐变颜色或停顿，等等）。在这种情况下，框架不知道该怎么做。我们将在本文的第二和第三部分中讨论转换矩阵和视图变化。目前，让我们把重点放在形状(shapes)上。</p>
<h2 id="形状路径的动画化"><a href="#形状路径的动画化" class="headerlink" title="形状路径的动画化"></a>形状路径的动画化</h2><p>想象一下，你有一个形状，使用路径来绘制一个规则的多边形。我们的实现当然会让你指出这个多边形将有多少条边。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">PolygonShape</span>(sides: <span class="number">3</span>).stroke(<span class="type">Color</span>.blue, lineWidth: <span class="number">3</span>)</span><br><span class="line"><span class="type">PolygonShape</span>(sides: <span class="number">4</span>).stroke(<span class="type">Color</span>.purple, lineWidth: <span class="number">4</span>)</span><br></pre></td></tr></table></figure>
<p><img src="https://images.xiaozhuanlan.com/photo/2022/af3cc99556499081e410cfed1a59cf16.png" alt=""></p>
<p>下面是我们的<code>PolygonShape</code>的实现。请注意，我使用了一点三角学的知识。这对理解这篇文章的主题并不重要，但如果你想了解更多关于它的信息，我写了另一篇文章，阐述了基础知识。你可以在 “<a href="https://swiftui-lab.com/trigonometric-recipes-for-swiftui/" target="_blank" rel="noopener">SwiftUI 的三角公式</a> “中阅读更多内容。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">PolygonShape</span>: <span class="title">Shape</span> </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> sides: <span class="type">Int</span></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">path</span><span class="params">(<span class="keyword">in</span> rect: CGRect)</span></span> -&gt; <span class="type">Path</span> &#123;        </span><br><span class="line">        <span class="comment">// hypotenuse</span></span><br><span class="line">        <span class="keyword">let</span> h = <span class="type">Double</span>(<span class="built_in">min</span>(rect.size.width, rect.size.height)) / <span class="number">2.0</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">// center</span></span><br><span class="line">        <span class="keyword">let</span> <span class="built_in">c</span> = <span class="type">CGPoint</span>(x: rect.size.width / <span class="number">2.0</span>, y: rect.size.height / <span class="number">2.0</span>)</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">var</span> path = <span class="type">Path</span>()</span><br><span class="line">                </span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="number">0</span>..&lt;sides &#123;</span><br><span class="line">            <span class="keyword">let</span> angle = (<span class="type">Double</span>(i) * (<span class="number">360.0</span> / <span class="type">Double</span>(sides))) * <span class="type">Double</span>.pi / <span class="number">180</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// Calculate vertex position</span></span><br><span class="line">            <span class="keyword">let</span> pt = <span class="type">CGPoint</span>(x: <span class="built_in">c</span>.x + <span class="type">CGFloat</span>(cos(angle) * h), y: <span class="built_in">c</span>.y + <span class="type">CGFloat</span>(sin(angle) * h))</span><br><span class="line">            </span><br><span class="line">            <span class="keyword">if</span> i == <span class="number">0</span> &#123;</span><br><span class="line">                path.move(to: pt) <span class="comment">// move to first vertex</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                path.addLine(to: pt) <span class="comment">// draw line to next vertex</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        path.closeSubpath()</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> path</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们可以更进一步，尝试使用与不透明度相同的方法对形状边数(<code>sides</code>)参数进行动画处理：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">PolygonShape</span>(sides: isSquare ? <span class="number">4</span> : <span class="number">3</span>)</span><br><span class="line">    .stroke(<span class="type">Color</span>.blue, lineWidth: <span class="number">3</span>)</span><br><span class="line">    .animation(.easeInOut(duration: duration))</span><br></pre></td></tr></table></figure>
<p>你认为 SwiftUI 将如何把三角形转化为正方形？你可能猜到了。它不会的。当然，框架不知道如何给它做动画。你可以随心所欲地使用<code>.animation()</code>，但这个形状会从三角形跳到正方形，而且没有任何动画。原因很简单：你只教了 SwiftUI 如何画一个 3 边的多边形，或 4 边的多边形，但你的代码却不知道如何画一个 3.379 边的多边形!</p>
<p>因此，为了使动画发生，我们需要两件事:</p>
<ol>
<li><p>我们需要改变形状的代码，使其知道如何绘制边数为非整数的多边形。</p>
</li>
<li><p>让框架多次生成这个形状，并让可动画参数一点点变化。也就是说，我们希望这个形状被要求绘制多次，每次都有一个不同的边数数值：3、3.1、3.15、3.2、3.25，一直到 4。</p>
</li>
</ol>
<p>一旦我们把这两点做到位，我们将能够在任何数量的边数之间制作动画:</p>
<p><img src="https://images.xiaozhuanlan.com/photo/2022/c12634ccd556d04558154aa3e50ba902.gif" alt=""></p>
<p><strong>创建可动画数据(<code>animatableData</code>)</strong></p>
<p>为了使形状可动画化，我们需要 SwiftUI 多次渲染视图，使用从原点到目标数之间的所有边值。幸运的是，<code>Shape</code>已经符合了<code>Animatable</code>协议的要求。这意味着，有一个计算的属性（<code>animatableData</code>），我们可以用它来处理这个任务。然而，它的默认实现被设置为<code>EmptyAnimatableData</code>。所以它什么都不做。</p>
<p>为了解决我们的问题，我们将首先改变边的属性的类型，从<code>Int</code>到<code>Double</code>。这样我们就可以有小数的数字。我们将在后面讨论如何保持该属性为<code>Int</code>，并仍然执行动画。但是现在，为了使事情简单，我们只使用<code>Double</code>。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">PolygonShape</span>: <span class="title">Shape</span> </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> sides: <span class="type">Double</span></span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后，我们需要创建我们的计算属性<code>animatableData</code>。在这种情况下，它非常简单。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">PolygonShape</span>: <span class="title">Shape</span> </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> sides: <span class="type">Double</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> animatableData: <span class="type">Double</span> &#123;</span><br><span class="line">        <span class="keyword">get</span> &#123; <span class="keyword">return</span> sides &#125;</span><br><span class="line">        <span class="keyword">set</span> &#123; sides = newValue &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>用小数画边</strong></p>
<p>最后，我们需要教 SwiftUI 如何绘制一个边数为非整数的多边形。我们将稍微改变我们的代码。随着小数部分的增长，这个新的边将从零到全长。其他顶点将相应地平稳地重新定位。这听起来很复杂，但这是一个最小的变化。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">path</span><span class="params">(<span class="keyword">in</span> rect: CGRect)</span></span> -&gt; <span class="type">Path</span> &#123;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// hypotenuse</span></span><br><span class="line">        <span class="keyword">let</span> h = <span class="type">Double</span>(<span class="built_in">min</span>(rect.size.width, rect.size.height)) / <span class="number">2.0</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">// center</span></span><br><span class="line">        <span class="keyword">let</span> <span class="built_in">c</span> = <span class="type">CGPoint</span>(x: rect.size.width / <span class="number">2.0</span>, y: rect.size.height / <span class="number">2.0</span>)</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">var</span> path = <span class="type">Path</span>()</span><br><span class="line">                </span><br><span class="line">        <span class="keyword">let</span> extra: <span class="type">Int</span> = <span class="type">Double</span>(sides) != <span class="type">Double</span>(<span class="type">Int</span>(sides)) ? <span class="number">1</span> : <span class="number">0</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="number">0</span>..&lt;<span class="type">Int</span>(sides) + extra &#123;</span><br><span class="line">            <span class="keyword">let</span> angle = (<span class="type">Double</span>(i) * (<span class="number">360.0</span> / <span class="type">Double</span>(sides))) * <span class="type">Double</span>.pi / <span class="number">180</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// Calculate vertex</span></span><br><span class="line">            <span class="keyword">let</span> pt = <span class="type">CGPoint</span>(x: <span class="built_in">c</span>.x + <span class="type">CGFloat</span>(cos(angle) * h), y: <span class="built_in">c</span>.y + <span class="type">CGFloat</span>(sin(angle) * h))</span><br><span class="line">            </span><br><span class="line">            <span class="keyword">if</span> i == <span class="number">0</span> &#123;</span><br><span class="line">                path.move(to: pt) <span class="comment">// move to first vertex</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                path.addLine(to: pt) <span class="comment">// draw line to next vertex</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        path.closeSubpath()</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> path</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>完整的代码可在文章顶部链接的 gist 文件中以 <strong>Example1</strong> 的形式提供。</p>
<p>如前所述，对于我们这个形状的用户来说，边的参数是一个<code>Double</code>，这可能显得很奇怪。人们应该期望边是一个<code>Int</code>参数。幸运的是，我们可以再次改变我们的代码，把这个事实隐藏在我们的形状的实现中：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">PolygonShape</span>: <span class="title">Shape</span> </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> sides: <span class="type">Int</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">var</span> sidesAsDouble: <span class="type">Double</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">var</span> animatableData: <span class="type">Double</span> &#123;</span><br><span class="line">        <span class="keyword">get</span> &#123; <span class="keyword">return</span> sidesAsDouble &#125;</span><br><span class="line">        <span class="keyword">set</span> &#123; sidesAsDouble = newValue &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">init</span>(sides: <span class="type">Int</span>) &#123;</span><br><span class="line">        <span class="keyword">self</span>.sides = sides</span><br><span class="line">        <span class="keyword">self</span>.sidesAsDouble = <span class="type">Double</span>(sides)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>有了这些变化，我们在内部使用<code>Double</code>，但在外部则使用<code>Int</code>。现在它看起来更优雅了。不要忘记修改绘图代码，这样它就会使用<code>sidesAsDouble</code> 而不是<code>sides</code>。完整的代码可以在文章顶部链接的 gist 文件中的 <strong>Example2</strong> 中找到。</p>
<h2 id="设置多个参数的动画"><a href="#设置多个参数的动画" class="headerlink" title="设置多个参数的动画"></a>设置多个参数的动画</h2><p>很多时候，我们会发现自己需要对一个以上的参数进行动画处理。单一的<code>Double</code>是不够的。在这些时候，我们可以使用<code>AnimatablePair&lt;First, Second&gt;</code>。这里，第一和第二都是符合<code>VectorArithmetic</code>的类型。例如<code>AnimatablePair&lt;CGFloat, Double&gt;</code>。</p>
<p><img src="https://images.xiaozhuanlan.com/photo/2022/a9d74ce65e124f4181701f5f5616253e.gif" alt=""></p>
<p>为了演示 <code>AnimatablePair</code> 的使用，我们将修改我们的例子。现在我们的多边形形状将有两个参数：边和比例。两者都将用<code>Double</code>来表示。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">PolygonShape</span>: <span class="title">Shape</span> </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> sides: <span class="type">Double</span></span><br><span class="line">    <span class="keyword">var</span> scale: <span class="type">Double</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">var</span> animatableData: <span class="type">AnimatablePair</span>&lt;<span class="type">Double</span>, <span class="type">Double</span>&gt; &#123;</span><br><span class="line">        <span class="keyword">get</span> &#123; <span class="type">AnimatablePair</span>(sides, scale) &#125;</span><br><span class="line">        <span class="keyword">set</span> &#123;</span><br><span class="line">            sides = newValue.first</span><br><span class="line">            scale = newValue.second</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>完整的代码可在文章顶部链接的 gist 文件中的 <strong>Example3</strong> 中找到。同一个文件中的<strong>Example4</strong>，有一个更复杂的路径。它基本上是相同的形状，但增加了一条连接每个顶点的线。</p>
<p><img src="https://images.xiaozhuanlan.com/photo/2022/cc060bf316c70d480d9c64dcf75f562d.gif" alt=""></p>
<h2 id="超过两个可动画的参数"><a href="#超过两个可动画的参数" class="headerlink" title="超过两个可动画的参数"></a>超过两个可动画的参数</h2><p>如果你浏览一下 SwiftUI 的声明文件，你会发现该框架相当广泛地使用<code>AnimatablePair</code>。比如说。<code>CGSize</code>、<code>CGPoint</code>、<code>CGRect</code>。尽管这些类型不符合<code>VectorArithmetic</code>，但它们可以被动画化，因为它们确实符合<code>Animatable</code>。</p>
<p>他们都以这样或那样的方式使用<code>AnimatablePair</code>：<br><figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">CGPoint</span> : <span class="title">Animatable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">typealias</span> <span class="type">AnimatableData</span> = <span class="type">AnimatablePair</span>&lt;<span class="type">CGFloat</span>, <span class="type">CGFloat</span>&gt;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">var</span> animatableData: <span class="type">CGPoint</span>.<span class="type">AnimatableData</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">CGSize</span> : <span class="title">Animatable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">typealias</span> <span class="type">AnimatableData</span> = <span class="type">AnimatablePair</span>&lt;<span class="type">CGFloat</span>, <span class="type">CGFloat</span>&gt;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">var</span> animatableData: <span class="type">CGSize</span>.<span class="type">AnimatableData</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">CGRect</span> : <span class="title">Animatable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">typealias</span> <span class="type">AnimatableData</span> = <span class="type">AnimatablePair</span>&lt;<span class="type">CGPoint</span>.<span class="type">AnimatableData</span>, <span class="type">CGSize</span>.<span class="type">AnimatableData</span>&gt;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">var</span> animatableData: <span class="type">CGRect</span>.<span class="type">AnimatableData</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>如果你仔细注意一下 <code>CGRect</code>，你会发现它实际上是在使用：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">AnimatablePair</span>&lt;<span class="type">AnimatablePair</span>&lt;<span class="type">CGFloat</span>, <span class="type">CGFloat</span>&gt;, <span class="type">AnimatablePair</span>&lt;<span class="type">CGFloat</span>, <span class="type">CGFloat</span>&gt;&gt;</span><br></pre></td></tr></table></figure>
<p>这意味着矩形的 x、y、宽度和高度值可以通过 <code>first.first</code>、<code>first.second</code>、<code>second.first</code>和<code>second.second</code>访问。</p>
<h2 id="使你自己的类型动画化（通过VectorArithmetic）"><a href="#使你自己的类型动画化（通过VectorArithmetic）" class="headerlink" title="使你自己的类型动画化（通过VectorArithmetic）"></a>使你自己的类型动画化（通过<code>VectorArithmetic</code>）</h2><p>以下类型默认实现了 <code>Animatable</code> : <code>Angle</code>, <code>CGPoint</code>, <code>CGRect</code>, <code>CGSize</code>, <code>EdgeInsets</code>, <code>StrokeStyle</code> 和 <code>UnitPoint</code>。以下类型符合<code>VectorArithmetic</code>。<code>AnimatablePair</code>, <code>CGFloat</code>, <code>Double</code>, <code>EmptyAnimatableData</code> 和 <code>Float</code>。你可以使用它们中的任何一种来为你的形状制作动画。</p>
<p>现有的类型提供了足够的灵活性来实现任何东西的动画。然而，如果你发现自己有一个想做动画的复杂类型，没有什么能阻止你添加自己的<code>VectorArithmetic</code>协议的实现。事实上，我们将在下一个例子中这样做。</p>
<p>为了说明这一点，我们将创建一个模拟时钟形状。它将根据一个自定义的可动画的参数类型移动它的指针：<code>ClockTime</code>。</p>
<p><img src="https://images.xiaozhuanlan.com/photo/2022/631640b6fd2099e4ccd9bcce295dade7.gif" alt=""></p>
<p>我们将像这样使用它：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">ClockShape</span>(clockTime: show ? <span class="type">ClockTime</span>(<span class="number">9</span>, <span class="number">51</span>, <span class="number">15</span>) : <span class="type">ClockTime</span>(<span class="number">9</span>, <span class="number">55</span>, <span class="number">00</span>))</span><br><span class="line">    .stroke(<span class="type">Color</span>.blue, lineWidth: <span class="number">3</span>)</span><br><span class="line">    .animation(.easeInOut(duration: duration))</span><br></pre></td></tr></table></figure>
<p>首先，我们开始创建我们的自定义类型<code>ClockTime</code>。它包含三个属性（小时、分钟和秒），几个有用的初始化器，以及一些辅助计算的属性和方法。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">ClockTime</span> </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> hours: <span class="type">Int</span>      <span class="comment">// Hour needle should jump by integer numbers</span></span><br><span class="line">    <span class="keyword">var</span> minutes: <span class="type">Int</span>    <span class="comment">// Minute needle should jump by integer numbers</span></span><br><span class="line">    <span class="keyword">var</span> seconds: <span class="type">Double</span> <span class="comment">// Second needle should move smoothly</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// Initializer with hour, minute and seconds</span></span><br><span class="line">    <span class="keyword">init</span>(<span class="number">_</span> h: <span class="type">Int</span>, <span class="number">_</span> m: <span class="type">Int</span>, <span class="number">_</span> s: <span class="type">Double</span>) &#123;</span><br><span class="line">        <span class="keyword">self</span>.hours = h</span><br><span class="line">        <span class="keyword">self</span>.minutes = m</span><br><span class="line">        <span class="keyword">self</span>.seconds = s</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// Initializer with total of seconds</span></span><br><span class="line">    <span class="keyword">init</span>(<span class="number">_</span> seconds: <span class="type">Double</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> h = <span class="type">Int</span>(seconds) / <span class="number">3600</span></span><br><span class="line">        <span class="keyword">let</span> m = (<span class="type">Int</span>(seconds) - (h * <span class="number">3600</span>)) / <span class="number">60</span></span><br><span class="line">        <span class="keyword">let</span> s = seconds - <span class="type">Double</span>((h * <span class="number">3600</span>) + (m * <span class="number">60</span>))</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">self</span>.hours = h</span><br><span class="line">        <span class="keyword">self</span>.minutes = m</span><br><span class="line">        <span class="keyword">self</span>.seconds = s</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// compute number of seconds</span></span><br><span class="line">    <span class="keyword">var</span> asSeconds: <span class="type">Double</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="type">Double</span>(<span class="keyword">self</span>.hours * <span class="number">3600</span> + <span class="keyword">self</span>.minutes * <span class="number">60</span>) + <span class="keyword">self</span>.seconds</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// show as string</span></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">asString</span><span class="params">()</span></span> -&gt; <span class="type">String</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="type">String</span>(format: <span class="string">"%2i"</span>, <span class="keyword">self</span>.hours) + <span class="string">":"</span> + <span class="type">String</span>(format: <span class="string">"%02i"</span>, <span class="keyword">self</span>.minutes) + <span class="string">":"</span> + <span class="type">String</span>(format: <span class="string">"%02f"</span>, <span class="keyword">self</span>.seconds)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>现在，为了符合<code>VectorArithmetic</code>协议，我们需要编写以下方法和计算属性：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">ClockTime</span>: <span class="title">VectorArithmetic</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">var</span> zero: <span class="type">ClockTime</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="type">ClockTime</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> magnitudeSquared: <span class="type">Double</span> &#123; <span class="keyword">return</span> asSeconds * asSeconds &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">static</span> <span class="function"><span class="keyword">func</span> -= <span class="params">(lhs: <span class="keyword">inout</span> ClockTime, rhs: ClockTime)</span></span> &#123;</span><br><span class="line">        lhs = lhs - rhs</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">static</span> <span class="function"><span class="keyword">func</span> - <span class="params">(lhs: ClockTime, rhs: ClockTime)</span></span> -&gt; <span class="type">ClockTime</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="type">ClockTime</span>(lhs.asSeconds - rhs.asSeconds)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">static</span> <span class="function"><span class="keyword">func</span> += <span class="params">(lhs: <span class="keyword">inout</span> ClockTime, rhs: ClockTime)</span></span> &#123;</span><br><span class="line">        lhs = lhs + rhs</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">static</span> <span class="function"><span class="keyword">func</span> + <span class="params">(lhs: ClockTime, rhs: ClockTime)</span></span> -&gt; <span class="type">ClockTime</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="type">ClockTime</span>(lhs.asSeconds + rhs.asSeconds)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">mutating</span> <span class="function"><span class="keyword">func</span> <span class="title">scale</span><span class="params">(by rhs: Double)</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> s = <span class="type">Double</span>(<span class="keyword">self</span>.asSeconds)</span><br><span class="line">        s.scale(by: rhs)</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">let</span> ct = <span class="type">ClockTime</span>(s)</span><br><span class="line">        <span class="keyword">self</span>.hours = ct.hours</span><br><span class="line">        <span class="keyword">self</span>.minutes = ct.minutes</span><br><span class="line">        <span class="keyword">self</span>.seconds = ct.seconds</span><br><span class="line">    &#125;    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>唯一要做的，就是写出形状来适当地定位针头。时钟形状的完整代码，可在本文顶部链接的gist文件中的  <strong>Example5</strong> 中找到。</p>
<h1 id="SwiftUI-Metal"><a href="#SwiftUI-Metal" class="headerlink" title="SwiftUI + Metal"></a>SwiftUI + Metal</h1><p>如果你发现自己正在编写复杂的动画，你可能会开始看到你的设备受到影响，同时试图跟上所有的绘图。如果是这样，你肯定会从启用金属的使用中受益。这里有一个例子，说明启用 Metal 后，一切都会变得不同。</p>
<p><img src="https://images.xiaozhuanlan.com/photo/2022/1f7298958b84e19cf6b5ca133e2c8fd5.gif" alt=""></p>
<p>在模拟器上运行时，你可能感觉不到有什么不同。然而，在真正的设备上，你会发现。视频演示来自iPad第六代（2016）。完整的代码在 gist 文件中，名称为 <strong>Example6</strong>。</p>
<p>幸运的是，启用 Metal，是非常容易的。你只需要添加 <code>.drawingGroup()</code> 修饰符:</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">FlowerView</span>().drawingGroup()</span><br></pre></td></tr></table></figure>
<p>根据 WWDC 2019, Session 237（<a href="https://developer.apple.com/videos/play/wwdc2019/237/" target="_blank" rel="noopener">用SwiftUI构建自定义视图</a>）：绘图组是一种特殊的渲染方式，但只适用于图形等东西。它基本上会将 SwiftUI 视图平铺到一个单一的 NSView/UIView 中，并用 Metal 进行渲染。跳到 WWDC 视频到37:27 了解更多细节。</p>
<p>如果你想尝试一下，但你的形状还没有复杂到让设备挣扎的地步，添加一些渐变和阴影，你会立即看到不同。</p>
<h1 id="接下来有什么内容？"><a href="#接下来有什么内容？" class="headerlink" title="接下来有什么内容？"></a>接下来有什么内容？</h1><p>在本文的第二部分，我们将学习如何使用 <code>GeometryEffect</code> 协议。它将打开改变我们的视图和动画的新方法的大门。与 <code>Paths</code> 一样，SwiftUI 没有关于如何在两个不同的变换矩阵之间转换的内置知识。<code>GeometryEffect</code>将有助于我们这样做。</p>
<p>目前，SwiftUI 没有关键帧功能。我们将看到我们如何用一个基本的动画来模拟一个。</p>
<p>在文章的第三部分，我们将介绍AnimatableModifier，这是一个非常强大的工具，它可以让我们对视图中任何可以变化的东西进行动画处理，甚至是文本！在这个系列的第三部分中，我们将介绍一些动画实例。关于这三部分系列中的一些动画例子，请看下面的视频: </p>
<p><a href="https://swiftui-lab.com/wp-content/uploads/2019/08/animations.mp4" target="_blank" rel="noopener">https://swiftui-lab.com/wp-content/uploads/2019/08/animations.mp4</a></p>
<blockquote>
<p>译自 The SwiftUI Lab 的 <a href="https://swiftui-lab.com/swiftui-animations-part1/" target="_blank" rel="noopener">Advanced SwiftUI Animations – Part 1: Paths</a></p>
<p>本文的完整示例代码可在以下网址找到：<br><a href="https://gist.github.com/swiftui-lab/e5901123101ffad6d39020cc7a810798" target="_blank" rel="noopener">https://gist.github.com/swiftui-lab/e5901123101ffad6d39020cc7a810798</a></p>
<p>示例8 需要的图片资源，可在这里下载：<br><a href="https://swiftui-lab.com/?smd_process_download=1&amp;download_id=916" target="_blank" rel="noopener">https://swiftui-lab.com/?smd_process_download=1&amp;download_id=916</a></p>
</blockquote>

      
    </div>
    
    
    
    

    
      <div>
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      </div>
    


    
      <div>
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/高级-SwiftUI-动画-—-Part-1：Paths/">高级 SwiftUI 动画 — Part 1：Paths</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 Swift社区 的个人博客">Swift社区</a></p>
  <p><span>发布时间:</span>2022年04月06日 - 10:04</p>
  <p><span>最后更新:</span>2022年04月06日 - 10:04</p>
  <p><span>原始链接:</span><a href="/高级-SwiftUI-动画-—-Part-1：Paths/" title="高级 SwiftUI 动画 — Part 1：Paths">https://fanbaoying.github.io/高级-SwiftUI-动画-—-Part-1：Paths/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://fanbaoying.github.io/高级-SwiftUI-动画-—-Part-1：Paths/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
        });
    });  
</script>

      </div>
    


    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat.png" alt="Swift社区 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.png" alt="Swift社区 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/SwiftUI/" rel="tag"><i class="fa fa-tag"></i> SwiftUI</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/LeetCode-29-两数相除/" rel="next" title="LeetCode - 29 两数相除">
                <i class="fa fa-chevron-left"></i> LeetCode - 29 两数相除
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/高级-SwiftUI-动画-—-Part-2：GeometryEffect/" rel="prev" title="高级 SwiftUI 动画 — Part 2：GeometryEffect">
                高级 SwiftUI 动画 — Part 2：GeometryEffect <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://avatars.githubusercontent.com/u/84354365?v=4"
                alt="Swift社区" />
            
              <p class="site-author-name" itemprop="name">Swift社区</p>
              <p class="site-description motion-element" itemprop="description">我们的使命是做一个最专业最权威的 Swift 中文社区，我们的愿景是希望更多的人学习和使用Swift。我们会分享以 Swift 实战、SwiftUI、Swift 基础为核心的技术干货，不忘初心，牢记使命。</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">187</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_36478920" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/5a30d987f265da430d580126" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-spinner"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/fanbaoying" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/swift-community" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/7711465033" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i>微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAxNzgzNTgwMw==&action=getalbum&album_id=2024314764775981057" target="_blank" title="公众号">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>公众号</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftWeekly" title="SwiftWeekly" target="_blank">SwiftWeekly</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/article-ios" title="Article-ios" target="_blank">Article-ios</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftUI-Book" title="SwiftUI-Book" target="_blank">SwiftUI-Book</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#显式动画-VS-隐式动画"><span class="nav-number">2.</span> <span class="nav-text">显式动画 VS 隐式动画</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#动画是如何工作的"><span class="nav-number">2.1.</span> <span class="nav-text">动画是如何工作的</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#为什么我关心-Animatable？"><span class="nav-number">3.</span> <span class="nav-text">为什么我关心 Animatable？</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#形状路径的动画化"><span class="nav-number">3.1.</span> <span class="nav-text">形状路径的动画化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#设置多个参数的动画"><span class="nav-number">3.2.</span> <span class="nav-text">设置多个参数的动画</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#超过两个可动画的参数"><span class="nav-number">3.3.</span> <span class="nav-text">超过两个可动画的参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使你自己的类型动画化（通过VectorArithmetic）"><span class="nav-number">3.4.</span> <span class="nav-text">使你自己的类型动画化（通过VectorArithmetic）</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#SwiftUI-Metal"><span class="nav-number">4.</span> <span class="nav-text">SwiftUI + Metal</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#接下来有什么内容？"><span class="nav-number">5.</span> <span class="nav-text">接下来有什么内容？</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Swift社区</span>

  
</div>

<!-- 
<div class="powered-by">
  <i class="fa fa-user-md">
  </i>
  <span id="busuanzi_container_site_uv">
    已有<span id="busuanzi_value_site_uv"></span>人来访
  </span>

</div>


  <span class="post-meta-divider">|</span>


<div class="powered-by">
  <i class="fa fa-eye"></i>
  <span id="busuanzi_container_site_pv">
      访问量<span id="busuanzi_value_site_pv"></span>次
  </span>
</div>



  <span class="post-meta-divider">|</span>


-->

<div class="theme-info">
  <i class="fa fa-pencil"></i>
  <span class="post-count">博客全站共361.6k字</span>
</div>


<!-- 
  <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>



-->

        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
